package cn.botter.subject.domain.handler.subject.impl;

import cn.botter.subject.common.enums.IsDeletedEnum;
import cn.botter.subject.common.enums.SubjectInfoTypeEnum;
import cn.botter.subject.domain.convert.MultipleSubjectConverter;
import cn.botter.subject.domain.convert.RadioSubjectConverter;
import cn.botter.subject.domain.entity.SubjectAnswerBO;
import cn.botter.subject.domain.entity.SubjectInfoBO;
import cn.botter.subject.domain.handler.subject.SubjectTypeHandler;
import cn.botter.subject.infra.basic.entity.SubjectMultiple;
import cn.botter.subject.infra.basic.entity.SubjectRadio;
import cn.botter.subject.infra.basic.service.SubjectMultipleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectMultipleService service;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //多选题目的插入
        //获得info表的id即subjectId;
        Integer SubjectId = subjectInfoBO.getId();
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        if (CollectionUtils.isEmpty(optionList)){
            log.info("Subject.Radio.add options isEmpty!");
            return;
        }
        optionList.forEach(option->{
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE.convertBoToRadio(option);
            subjectMultiple.setSubjectId(SubjectId);
            subjectMultiple.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);
        });

        service.bathInsert(subjectMultipleList);
    }

    @Override
    public SubjectInfoBO query(Integer id) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(id);
        List<SubjectMultiple> result = service.queryByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOList = MultipleSubjectConverter.INSTANCE.convertRadioToAnswerBO(result);


        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        subjectInfoBO.setOptionList(subjectAnswerBOList);
        return subjectInfoBO;
    }
}
